package com.ftg.learn.framework.mybatis.annotation;

import com.mysql.cj.Session;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;

/**
 * 练习
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/9/4 上午11:11
 */
public class MybatisExample2 {

    public static void main(String[] args) throws IOException {

        String resource = "zong.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //把接口添加到缓存里面
        sqlSessionFactory.getConfiguration().addMapper(Student.class);

        SqlSession session = null;
        try {
            session = sqlSessionFactory.openSession();
            //从缓存里面拿到这个接口

            Student mapper = session.getMapper(Student.class);

            System.out.println(mapper.show());

            //映射器 不是.xml 而是一个interface
            session.commit();
        } catch (Exception e) {
            session.rollback();
        }finally {
            session.close();
        }
    }
}
